package com.smart.client.core.initial;

import com.smart.client.core.client.IClient;
import com.smart.client.core.handler.NettyChannelInboundHandlerAdapter;
import com.smart.client.core.notice.EventNotice;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import lombok.RequiredArgsConstructor;
import org.springframework.lang.NonNull;

/**
 * Created By Li Jiaxi
 * <br/>Date: 2023/4/12 10:36
 * <br/>Description:
 */
// @Component
@RequiredArgsConstructor
public class NettyChannelInitializer extends ChannelInitializer<Channel> {

    /**
     * 通道拦截处理器
     */
    // private final ChannelInboundHandlerAdapter channelInboundHandlerAdapter;

    private final IClient client;

    private final EventNotice eventNotice;

    @Override
    protected void initChannel(@NonNull Channel channel) throws Exception {
        // ChannelOutboundHandler，依照逆序执行（从下往上）
        channel.pipeline().addLast("encoder", new StringEncoder());
        // 属于ChannelInboundHandler，依照顺序执行（从上往下）
        channel.pipeline().addLast("decoder", new StringDecoder());
        // channel.pipeline().addLast(new LoggingHandler(LogLevel.INFO));
        /**
         * 自定义ChannelInboundHandlerAdapter
         * 该项在源码中放在了上面两项上面，实际上需要调整到后面
         */
        channel.pipeline().addLast(new NettyChannelInboundHandlerAdapter(eventNotice, client));
    }
}
